package com.coins.common.redis;

import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.coins.common.jobs.JobMessage;
import com.coins.common.jobs.JobPools;

@Component
public class RedisListenerHandler {

	protected static final Logger LOGGER = LoggerFactory.getLogger(RedisListenerHandler.class);

	@Autowired
	private JobPools jobPools;

	@Autowired
	private CountDownLatch latch;

	@Autowired
	public RedisListenerHandler(CountDownLatch latch) {
		this.latch = latch;
	}

	public void onMessage(String message) {
		JSONArray json = JSONArray.parseArray(message);
		if (json.size() > 0) {
			JSONObject o = (JSONObject) json.get(1);
			o.remove("handler");
			JobMessage msg = o.toJavaObject(JobMessage.class);
			jobPools.addJobMessage(msg);
		}
		latch.countDown();
	}
}